using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

[Serializable]
public class RandomMoveComponent : BaseComponent
{
    [Header("资源爆炸效果设置")]
    public float explosionHeightOffset = 1.0f;  // 爆炸效果的高度偏移
    public float explosionMaxDistance = 1.5f;         // 爆炸力度
    public float explosionMinDistance = 0.6f;        // 旋转力度
    public float duration = 0.5f;
    public float MaxAngle;
    public float MinAngle;
    public void PlayRandomMove(Action<Entity> CompleteAction)
    {
        // 计算随机偏移方向
        float angle = UnityEngine.Random.Range(MinAngle, MaxAngle) * Mathf.Deg2Rad;
        float distance = UnityEngine.Random.Range(explosionMinDistance, explosionMaxDistance);
        Vector3 randomOffset = new Vector3(
            Mathf.Cos(angle) * distance,
            0,
            Mathf.Sin(angle) * distance
        );

        // 生成初始位置（略微抬高）
        Vector3 spawnPosition = entity.transform.position + Vector3.up * explosionHeightOffset;
        // 创建一个随机的Y轴旋转
        Quaternion randomRotation = Quaternion.Euler(0, UnityEngine.Random.Range(0f, 360f), 0);

        // 计算目标落点
        Vector3 targetPosition = spawnPosition + randomOffset;
        targetPosition.y = 0;

        // 创建一条优美的抛物线路径
        float height = UnityEngine.Random.Range(1.5f, 2f);
        Vector3[] path = new Vector3[3];
        path[0] = spawnPosition;  // 起点
        path[1] = Vector3.Lerp(spawnPosition, targetPosition, 0.5f) + Vector3.up * height;  // 控制点
        path[2] = targetPosition;  // 终点

        // 使用DOPath创建平滑的路径动画
        entity.transform.DOPath(path, duration, PathType.CatmullRom)
            .SetEase(Ease.OutQuad)  // 使用OutQuad缓动使动作更加自然
            .OnComplete(() =>
            {
                // 添加一个很小的弹跳效果
                entity.transform.DOMoveY(
                    targetPosition.y + 0.1f,  // 稍微弹起一点
                    0.1f
                ).SetLoops(2, LoopType.Yoyo)  // 上下弹跳一次
                .SetEase(Ease.OutQuad)
                .OnComplete(() =>
                {
                    CompleteAction?.Invoke(entity);
                });
            });
            entity.transform.DORotateQuaternion(randomRotation, duration).SetEase(Ease.OutQuad);
    }
}
